﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Web.Configuration;

namespace Enterprise.UI.Web.Modules.WebEmail
{
    public partial class DownAttch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            object p = Request.QueryString["p"];
            if (p != null && !string.IsNullOrEmpty(p.ToString()))
            {
                Enterprise.Infrastructure.CryptographerHelper cry = new Infrastructure.CryptographerHelper();
                //string urldecode = HttpContext.Current.Server.UrlDecode(p.ToString());
                string filepath = cry.Decrypt(p.ToString());
                string file = ConfigurationManager.AppSettings["EmailAttachmentsPath_Inbox"] + filepath;
                string filename = System.IO.Path.GetFileName(file);
                if (System.IO.File.Exists(file))
                {
                    FileStream MyFileStream;
                    long FileSize;
                    MyFileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //比如E\\mydata.doc
                    FileSize = MyFileStream.Length;
                    byte[] Buffer = new byte[(int)FileSize];
                    MyFileStream.Read(Buffer, 0, (int)FileSize);
                    MyFileStream.Close();
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + "\"");
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(Buffer);
                    Response.Flush();
                    Response.Close();
                }
            }
        }
    }
}